<template>
  <div class="main-panel" style="padding: 50px">
    <el-tabs v-model="activeName">
      <el-tab-pane label="个人信息" name="info">
        <el-form ref="updUserform" :model="form" :rules="formRules" label-width="80px" style="width: 500px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio v-model="form.sex" :label="0">男</el-radio>
            <el-radio v-model="form.sex" :label="1">女</el-radio>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input-number v-model="form.age" controls-position="right" :min="1" :max="200"></el-input-number>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updateUser">提交</el-button>
            <el-button @click="clearPwdForm('updUserform')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="修改密码" name="updPwd">
        <el-form ref="resetPwdform" :model="pwdForm" :rules="pwdFormRules" label-width="80px" style="width: 500px">
          <el-form-item label="原密码" required prop="oldPwd">
            <el-input v-model="pwdForm.oldPwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="新密码" required prop="newPwd">
            <el-input v-model="pwdForm.newPwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" required prop="pwd">
            <el-input v-model="pwdForm.pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updatePwd">提交</el-button>
            <el-button @click="clearPwdForm('resetPwdform')">清空</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getUesrById, updatePwd, editCurrentUser } from '_api/user'
export default {
  name: 'userCenter',
  created () {
    getUesrById({ id: this.$store.getters.userId }).then(res => {
      this.form = res
    })
  },
  data () {
    const validPwd = (rule, value, callback) => {
      if (!this.pwdForm.pwd) {
        callback(new Error('请输入密码'))
      } else if (this.pwdForm.newPwd !== this.pwdForm.pwd) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'info',
      form: {
        name: '',
        sex: 0,
        age: 1,
        phone: ''
      },
      formRules: {},
      pwdForm: {
        oldPwd: '',
        newPwd: '',
        pwd: ''
      },
      pwdFormRules: {
        oldPwd: [{ required: true, message: '原密码必填' }],
        newPwd: [{ required: true, message: '新密码不能为空' }],
        pwd: [{ required: true, validator: validPwd }]
      }
    }
  },
  methods: {
    updatePwd () {
      let self = this
      self.$refs.resetPwdform.validate(valid => {
        if (valid) {
          updatePwd(self.pwdForm).then(() => {
            self.$message.success('修改成功，请重新登录！')
            setTimeout(() => {
              self.$store.dispatch('user/logout').then(() => {
                location.reload()
              })
            }, 2000)
          })
        }
      })
    },
    updateUser () {
      let self = this
      self.$refs.updUserform.validate(valid => {
        if (valid) {
          editCurrentUser({
            id: self.form.id,
            name: self.form.name,
            sex: self.form.sex,
            age: self.form.age,
            phone: self.form.phone
          }).then(() => {
            self.$store.dispatch('user/getInfo')
            self.$message.success('修改成功')
          })
        }
      })
    },
    clearPwdForm (formName) {
      let self = this
      if (formName === 'updUserform') {
        getUesrById({ id: this.$store.getters.userId }).then(res => {
          this.form = res
        })
      } else {
        self.$refs[formName].resetFields()
      }
    }
  }
}
</script>

<style scoped>

</style>
